package com.yk.spark.service.file.impl;

import com.yk.spark.dao.common.api.ICommonDAO;
import com.yk.spark.dao.file.api.IFileDAO;
import com.yk.spark.model.FileModel;
import com.yk.spark.service.file.api.IFileService;
import com.yk.spark.util.SparkFileIO;
import com.yk.spark.util.UuidUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * Created by zsyoung on 2017/8/23.
 */
@Service("fileService")
public class FileServiceImpl implements IFileService {

    @Resource(name = "fileDAO")
    private IFileDAO fileDao;

    @Resource(name="commonDAO")
    private ICommonDAO commonDAO;

    private SparkFileIO fileIO=SparkFileIO.getSparkFileIo();

//    private static final String MASTER_PATH="hdfs://10.0.101.240:9000/";
    @Override
    public boolean deleteFile(String fileId) {
        FileModel model=new FileModel();
        model.setFileId(fileId);
        model=this.fileDao.queryFileModel(model);
        boolean result=false;
        try {
            this.fileIO.sparkDeleteFile(model.getFilePath(),model.getFileName());
            result=this.commonDAO.delete(model);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }

    @Override
    public boolean saveFile(InputStream inputStream, String filePath, String fileName,double size) {

        FileModel model=new FileModel();
        model.setFileName(fileName);
        model.setFilePath(filePath);
        model.setCreateTime(new Timestamp(new Date().getTime()));
        model.setUpdateTime(new Timestamp(new Date().getTime()));
        model.setSize(size);
        model.setRemark("客户端上传");
        boolean result=false;
        try {
            result=this.commonDAO.save(model);
            this.fileIO.sparkIOUpload(inputStream,model.getFilePath(),model.getFileName());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }

    @Override
    public boolean updateFile(String fileId, String filePath,String fileName,String remark) {
        FileModel model=new FileModel();
        model.setFileId(fileId);
        model=this.fileDao.queryFileModel(model);
        model.setFileName(fileName);
        model.setFilePath(filePath);
        model.setUpdateTime(new Timestamp(new Date().getTime()));
        model.setRemark(remark);
        return this.commonDAO.save(model);
    }

    /**
     * 获取文件实体
     *
     * @param fileId 文件Id
     * @return 文件实体
     */
    @Override
    public FileModel getFileModel(String fileId) {
        FileModel model=new FileModel();
        model.setFileId(fileId);
        return this.fileDao.queryFileModel(model);
    }

    /**
     * 获取所有文件实体
     *
     * @return 文件实体列表
     */
    @Override
    public List<FileModel> getFileModel() {
        return this.fileDao.queryFileModel();
    }

    /**
     * 获取分页文件实体
     *
     * @param start
     * @param limit
     * @return 文件实体列表
     */
    @Override
    public List<FileModel> getFileModel(int start, int limit) {
        return this.fileDao.queryFileModel(start,limit);
    }

    /**
     * 文件下载
     * @param fileId 文件对象的id
     */
    @Override
    public void downLoadFile(String fileId, BufferedOutputStream outputStream) throws IOException{
        FileModel model=new FileModel();
        model.setFileId(fileId); // 取得想要下载的文件对象的信息
        model=this.fileDao.queryFileModel(model);
        this.fileIO.sparkIODownload(model.getFilePath(),model.getFileName(),outputStream);
    }

    @Override
    public long getFileModelTotal() {
        return this.fileDao.queryFileModelTotal();
    }
}
